package 学生管理系统升级版;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class 学生管理系统test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<student> list = new ArrayList<>();
        ArrayList<user> listuser = new ArrayList<>();
        tool:
        while (true) {
            System.out.println("欢迎来到学生管理系统");
            System.out.println("请选择操作：");
            System.out.println("1.登录");
            System.out.println("2.注册");
            System.out.println("3.忘记密码");
            System.out.println("4.退出");
            String chose1 = sc.next();
            switch (chose1) {
                case "1" -> {
                    if (登录(listuser)) {
                        初始菜单(list, listuser);
                    }
                }
                case "2" -> 注册(listuser);
                case "3" -> {
                    忘记密码(listuser);
                    for (int i = 0; i < listuser.size(); i++) {
                        System.out.println(listuser.get(i).getUsername() + "," + listuser.get(i).getPhoneNumber() + "," + listuser.get(i).getMima());
                    }
                }
                case "4" -> {
                    System.out.println("您已退出学生管理平台");
                    System.out.println("感谢您的参与");
                    break tool;
                }
                default -> {
                    System.out.println("输入错误，没有这个选项");
                    System.out.println("请重新输入");
                }
            }
        }
    }

    //-----------------------------------------------------------------------------
    //添加学生功能
    public static ArrayList<student> add() {
        Scanner sc = new Scanner(System.in);
        ArrayList<student> list = new ArrayList<>();
        for (int i = 0; i < 4; ) {
            student stu = new student();
            System.out.println("请输入学生ID:");
            String s = sc.next();
            if (IDweiyi(list, s)) {
                stu.setId(s);
                System.out.println("请输入学生姓名：");
                stu.setName(sc.next());
                System.out.println("请输入学生年龄：");
                stu.setAge(sc.nextInt());
                System.out.println("请输入学生家庭住址：");
                stu.setHome(sc.next());
                list.add(stu);
                i++;
            } else {
                System.out.println("ID已存在,请重新输入：");
            }
        }
        System.out.println("添加操作结束,返回初始菜单");
        return list;
    }

    //判断ID唯一性
    public static boolean IDweiyi(ArrayList<student> list, String s) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId().equals(s)) {
                return false;
            }
        }
        return true;
    }

    public static boolean 判断用户名唯一(ArrayList<user> list, String s) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getUsername().equals(s)) {
                return false;
            }
        }
        return true;
    }

    //-----------------------------------------------------------------------------
    //删除学生功能
    public static ArrayList<student> remove(ArrayList<student> list, int Index) {
        list.remove(Index);
        return list;
    }

//------------------------------------------------------------------------------------------

    //判断id存在功能
    public static int panduanID(ArrayList<student> list, String removeID) {
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId().equals(removeID)) {
                return i;
            }
        }
        return -1;
    }

    //------------------------------------------------------------------------------------------
    //修改功能
    public static void set(ArrayList<student> list, int Index) {
        student s2 = list.get(Index);
        Scanner sc = new Scanner(System.in);
        ArrayList<student> list1 = remove(list, Index);
        while (true) {
            System.out.println("请输入修改后的id：");
            String newID = sc.next();
            int i = panduanID(list1, newID);
            if (i < 0) {
                s2.setId(newID);
                System.out.println("请输入修改后的姓名：");
                s2.setName(sc.next());
                System.out.println("请输入修改后的年龄：");
                s2.setAge(sc.nextInt());
                System.out.println("请输入修改后的家庭住址：");
                s2.setHome(sc.next());
                list.add(s2);
                break;
            } else {
                System.out.println("该ID已存在，请重新输入：");
            }
        }
    }

    public static ArrayList<user> 注册(ArrayList<user> listuser) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            user u = new user();
            System.out.println("请输入用户名：");
            String userName = sc.next();
            if (判断用户名组合(userName)) {
                if (判断用户名长度(userName)) {
                    if (判断用户名唯一(listuser, userName)) {
                        u.setUsername(userName);
                        System.out.println("请输入密码：");
                        String mima1 = sc.next();
                        System.out.println("请确认密码：");
                        String mima2 = sc.next();
                        if (验证密码(mima1, mima2)) {
                            u.setMima(mima2);
                            System.out.println("请输入身份证号：");
                            String personNumber = sc.next();
                            if (验证身份证号(personNumber)) {
                                u.setPersonNumber(personNumber);
                                System.out.println("请输入手机号：");
                                String phoneNumber = sc.next();
                                if (判断手机号(phoneNumber)) {
                                    u.setPhoneNumber(phoneNumber);
                                    listuser.add(u);
                                    System.out.println("注册成功");
                                    System.out.println("返回登录界面");
                                    return listuser;
                                }
                            }
                        }
                    }
                    else {
                        System.out.println("此用户名以被注册");
                        System.out.println("请重新输入");
                    }
                }
            }
        }


    }

    public static boolean 判断用户名长度(String username) {
        int len = username.length();
        if (len >= 3 && len <= 15) {
            return true;
        }
        System.out.println("用户名长度必须在3~15位之间");
        System.out.println("请重新输入");
        return false;
    }

    public static boolean 判断用户名组合(String username) {
        int zimuCount = 0;
        int numberCount = 0;
        for (int i = 0; i < username.length(); i++) {
            if ((username.charAt(i) >= 65 && username.charAt(i) <= 90) || (username.charAt(i) >= 97 && username.charAt(i) <= 122)) {
                zimuCount++;
            } else if (username.charAt(i) >= 48 && username.charAt(i) <= 57) {
                numberCount++;
            } else {
                System.out.println("输入错误！");
                System.out.println("您输入了字母和数字以外的字符");
                System.out.println("请重新输入");
                return false;
            }
        }
        if (zimuCount == username.length()) {
            System.out.println("输入错误");
            System.out.println("您输入的用户名全为字母");
            System.out.println("请重新输入");
            return false;
        } else if (numberCount == username.length()) {
            System.out.println("输入错误！");
            System.out.println("您输入的用户名全为数字");
            System.out.println("请重新输入");
            return false;
        } else {
            return true;
        }
    }

    public static boolean 验证密码(String mima1, String mima2) {
        if (mima1.length() == mima2.length()) {
            for (int i = 0; i < mima1.length(); i++) {
                if (mima1.charAt(i) != mima2.charAt(i)) {
                    System.out.println("两次输入的密码不统一");
                    System.out.println("请重新输入");
                    return false;
                }
            }
            return true;
        } else {
            System.out.println("两次输入的密码不统一");
            System.out.println("请重新输入");
            return false;
        }
    }

    public static boolean 验证身份证号(String personNumber) {
        if (personNumber.length() != 18) {
            System.out.println("您输入的身份证号长度不为18");
            System.out.println("请重新输入");
            return false;
        } else if (personNumber.charAt(0) == '0') {
            System.out.println("开头不能为零");
            System.out.println("请重新输入");
            return false;
        }
        for (int i = 1; i < personNumber.length() - 1; i++) {
            if (personNumber.charAt(i) < 48 || personNumber.charAt(i) > 57) {
                System.out.println("输入格式错误");
                System.out.println("请重新输入");
                return false;
            }
        }
        if ((personNumber.charAt(personNumber.length() - 1) < 48 || personNumber.charAt(personNumber.length() - 1) > 57) && (personNumber.charAt(personNumber.length() - 1) != 'x' && personNumber.charAt(personNumber.length() - 1) != 'X')) {
            System.out.println("输入格式错误，最后一位不能是数字和'X'或'x'以外的字");
            System.out.println("请重新输入");
        }
        return true;
    }

    public static boolean 判断手机号(String phoneNumber) {
        if (phoneNumber.length() != 11) {
            System.out.println("手机号长度错误");
            System.out.println("请重新输入");
            return false;
        } else if (phoneNumber.charAt(0) == '0') {
            System.out.println("开头不能为零");
            System.out.println("请重新输入");
            return false;
        }
        for (int i = 0; i < phoneNumber.length(); i++) {
            if (phoneNumber.charAt(i) < 48 || phoneNumber.charAt(i) > 57) {
                System.out.println("手机号只能由数字组成");
                System.out.println("请重新输入");
                return false;
            }
        }
        return true;
    }

    public static boolean 登录(ArrayList<user> listuser) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username = sc.next();
        System.out.println("请输入密码：");
        String mima = sc.next();
        tool:
        while (true) {
            System.out.print("请输入验证码：");
            String trueyanzhengma = 验证码生成();
            System.out.println("验证码:" + trueyanzhengma);
            String yanzhengma = sc.next();
            if (!判断用户名唯一(listuser, username)) {
                if (trueyanzhengma.equalsIgnoreCase(yanzhengma)) {
                    for (int i = 0; i < 3; ) {
                        for (int i1 = 0; i1 < listuser.size(); i1++) {
                            if (listuser.get(i1).getUsername().equals(username) && listuser.get(i1).getMima().equals(mima)) {
                                System.out.println("登录成功");
                                System.out.println("请等待进入学生管理系统");
                                return true;
                            }
                            if (i < 2) {
                                System.out.println("用户名或密码输入错误");
                                System.out.println("你还有" + (2 - i) + "次机会");
                                System.out.println("请重新输入");
                                System.out.println("请输入用户名：");
                                username = sc.next();
                                System.out.println("请输入密码：");
                                mima = sc.next();
                                i++;
                            } else {
                                System.out.println("输入错误次数过多");
                                System.out.println("封锁1分钟");
                                return false;
                            }
                        }
                    }
                } else {
                    System.out.println("验证码错误");
                    System.out.println("请重新输入");
                }
            } else {
                System.out.println("用户名未注册，请先注册");
                return false;
            }
        }
    }

    public static ArrayList<user> 忘记密码(ArrayList<user> listuser) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名：");
        String username = sc.next();
        if (!判断用户名唯一(listuser, username)) {
            System.out.println("请输入身份证号：");
            String personNumber = sc.next();
            System.out.println("请输入手机号码：");
            String phoneNumber = sc.next();
            for (int i = 0; i < listuser.size(); i++) {
                if (listuser.get(i).getPersonNumber().equalsIgnoreCase(personNumber) && listuser.get(i).getPhoneNumber().equals(phoneNumber)) {
                    while (true) {
                        user u = listuser.get(i);
                        System.out.println("请输入新密码:");
                        String newMima1 = sc.next();
                        System.out.println("请确认新密码:");
                        String newMima2 = sc.next();
                        if (验证密码(newMima1, newMima2)) {
                            u.setMima(newMima2);
                            listuser.set(i, u);
                            System.out.println("修改密码成功,返回登录界面");
                            return listuser;
                        }
                    }
                }
            }
            System.out.println("账号信息不匹配");
            System.out.println("修改失败");
            return listuser;
        }
        System.out.println("未注册");
        System.out.println("请先注册");
        return listuser;
    }

    public static void 初始菜单(ArrayList<student> list, ArrayList<user> listuser) {
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("-------------欢迎进入学生管理系统----------------");
            System.out.println("1：添加学生");
            System.out.println("2：删除学生");
            System.out.println("3：修改学生");
            System.out.println("4：查询学生");
            System.out.println("5：退出");
            System.out.println("请输入您的选择:");
            String chose2 = sc.next();
            if (chose2.equals("1")) {               //添加---------------------
                System.out.println("您已进入添加学生功能，请根据提示操作：");
                list = add();
            } else if (chose2.equals("2")) {         //删除-------------------
                System.out.println("您已进入删除学生功能，请根据提示操作：");
                System.out.println("请输入要删除的学生id：");
                String removeID = sc.next();
                if (panduanID(list, removeID) >= 0) {
                    int Index = panduanID(list, removeID);
                    remove(list, Index);
                    System.out.println("删除成功，返回初始菜单");
                } else {
                    System.out.println("此id不存在，返回初始菜单");
                }
            } else if (chose2.equals("3")) {      //修改-------------
                System.out.println("您已进入修改学生功能，请根据提示操作：");
                System.out.println("请输入要修改的学生id：");
                String setID = sc.next();
                if (panduanID(list, setID) >= 0) {
                    int Index = panduanID(list, setID);
                    set(list, Index);
                    System.out.println("修改成功");
                    System.out.println("修改操作结束，返回初始菜单");
                } else {
                    System.out.println("此id不存在，返回初始菜单");
                }
            } else if (chose2.equals("4")) {        //查询----------------
                if (list.isEmpty()) {
                    System.out.println("当前无学生信息，请添加后再查询");
                } else {
                    System.out.println("以下是已录入的学生信息:");
                    System.out.println("ID" + "    " + "姓名" + "    " + "年龄" + "    " + "家庭住址");
                    for (int i = 0; i < list.size(); i++) {
                        System.out.println(list.get(i).getId() + "    " + list.get(i).getName() + "    " + list.get(i).getAge() + "      " + list.get(i).getHome());
                    }
                    System.out.println("查询操作结束，返回初始菜单");
                }
            } else if (chose2.equals("5")) {        //退出---------------
                System.out.println("已为您退出学生管理系统");
                System.out.println("感谢您的参与");
                break;
            } else {
                System.out.println("输入错误");
                System.out.println("请重新输入：");
            }
        }
    }

    public static String 验证码生成() {
        Random r = new Random();
        StringBuilder sb1 = new StringBuilder();
        StringBuilder sb2 = new StringBuilder();
        for (int i = 0; i < 26; i++) {
            sb1.append((char) (97 + i)).append((char) (65 + i));
        }
        for (int j = 0; j < 4; j++) {
            int i = r.nextInt(52);
            sb2.append(sb1.toString().charAt(i));
        }
        int i = r.nextInt(10);
       sb2.append(i);
        String trueyanzhengma = sb2.toString();
        return trueyanzhengma;
    }
}